package com.wyy.service.impl;

import com.wyy.bean.Menu;
import com.wyy.bean.Role;
import com.wyy.dao.MiddleDao;
import com.wyy.dao.RoleDao;
import com.wyy.dao.impl.MiddleDaoImpl;
import com.wyy.dao.impl.RoleDaoImpl;
import com.wyy.service.RoleService;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @ClassName RoleServiceImpl
 * @Description //TODO
 * @Author wky
 * @Date 2021/9/27 8:19
 * @Version 1.0
 **/
public class RoleServiceImpl implements RoleService {
    
    private RoleDao roleDao = new RoleDaoImpl();
    private MiddleDao middleDao=new MiddleDaoImpl();
    
    @Override
    public List<Role> getList() {
        return roleDao.getList();
    }
    
    @Override
    public List<Role> getRoleList(int pageIndex, int pageSize) {
        return roleDao.getRoleList(pageIndex, pageSize);
    }
    
    @Override
    public int total() {
        return roleDao.total();
    }
    
    @Override
    public int insertRole(String roleName, String state, String[] ids) {

        int res = 0;
        try {
            Role role = new Role();
            role.setRoleName(roleName);
            role.setRoleState(Integer.parseInt(state));
            //1.新增角色数据
            int roleId = roleDao.insert(role); //得到新增数据的Id
            //2.关联角色与菜单
            middleDao.insertMiddle(roleId, ids);
            res = 1;
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return res;
    }

    @Override
    public int deleteRole(int roleId) {
        //删除中间表数据--对应菜单
        middleDao.deleteMiddle(roleId);
        //删除角色
        int i = roleDao.deleteRole(roleId);
        return i;
    }

    @Override
    public Role findById(int roleId) {
        return roleDao.findById(roleId);
    }

    @Override
    public int updateRole(Role role,String[] menuIds) {
        int res = 0;
        try {
            //1.更新角色
            roleDao.updateRole(role);
            //2.删除对应role中间表数据
            middleDao.deleteMiddle(role.getRoleId());
            //3.添加对应role中间表数据
            middleDao.insertMiddle(role.getRoleId(), menuIds);
            res = 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }
}
